package com.example.demo.synchronizationDemo;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * Created by 蔡超 on 2018/10/19.
 */
public class Demo3 {

    public static void test1(){
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                print("A");
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                print("B");
            }
        });

        t1.start();
        t2.start();

    }

    public static void test2(){
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                print("A");
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                print("B");
            }
        });

        t1.start();
        t2.start();
    }

    public static void test3(){
        Object lock = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println("AAAAAAAAAAAA");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("BBBBBBBBBBBBBB");
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println("T2.............");
                    lock.notify();
                }

            }
        });

        t1.start();
        t2.start();
    }

    private static CountDownLatch countDownLatch = new CountDownLatch(3);

    public static void test4(){
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1..........");
                countDownLatch.countDown();
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t2..........");
                countDownLatch.countDown();
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t3..........");
                countDownLatch.countDown();
            }
        });

        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t4..........");
            }
        });

        t4.start();
        t2.start();
        t1.start();
        t3.start();
    }

    public static void test5(){
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1准备..........");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("t1跑..........");
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t2准备..........");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("t2跑..........");
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t3准备..........");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("t3跑..........");
            }
        });

        t2.start();
        t1.start();
        t3.start();

    }

    public void test6(){

    }

    private static void print(String name){
        for (int i = 0; i < 3; i++) {
            System.out.println(name + " print " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();

    }
}
